package Constructions;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URLClassLoader;

import javax.imageio.ImageIO;

import Main.SpaceGame;
import Tasks.ActivateLifeSupport;

public class LifeSupport extends Construction
{
	boolean active = true;
	int x,y;
	int refillCnt = 5;
	BufferedImage sprite = null;
	SpaceGame main;

	public LifeSupport(int x,int y,SpaceGame main)
	{
		super(x,y,main);
		
		this.main = main;
		this.x = x;
		this.y = y;
		try {
			sprite = ImageIO.read(URLClassLoader.getSystemResource("Resources/"+"LifeSupport.png"));
		}
		catch (IOException e) {
			e.printStackTrace();
		}
		workingTile = main.world.tiles[x][y];
		
		setText();
	}

	@Override
	public void draw(Graphics g) 
	{
		g.drawImage(sprite, x*10, y*10, null);
		drawFlash(g,x,y);
	}
	
	public String getName()
	{
		return "Life Support";
	}
	
	@Override
	public void setText() 
	{
		colors.clear();
		text.clear();
		textColorIndex = 2;
		
		colors.add(Color.cyan);
		colors.add(Color.cyan);
		text.add("");
		text.add(getName());
		
		if(powered)
		{
			colors.add(Color.green);
			colors.add(Color.cyan);
			text.add("a");
			text.add(" - Turn on/off");
			
			colors.add(Color.cyan);
			colors.add(Color.cyan);
			text.add("");
			text.add("");
			
			if (active)
			{
				colors.add(Color.cyan);
				colors.add(Color.cyan);
				text.add("");
				text.add("Active");
			}
			else
			{
				colors.add(Color.red);
				colors.add(Color.red);
				text.add("");
				text.add("Not Active");
			}
		}
		
		else
		{
			colors.add(Color.cyan);
			colors.add(Color.cyan);
			text.add("");
			text.add("");
			
			colors.add(Color.red);
			colors.add(Color.red);
			text.add("Needs Power");
			text.add("");
		}
	}
	
	public void update()
	{
		checkPowered(x,y);
		if (active)
		{
			if(refillCnt <=0 && main.world.tiles[x][y].getOxygen()<100)
			{
				main.world.tiles[x][y].setOxygen(main.world.tiles[x][y].getOxygen()+6);
				refillCnt = 5;
			}
			else refillCnt--;
		}
	}
	
	public boolean isActive()
	{
		return active;
	}
	
	public void setActive(boolean a)
	{
		this.active = a;
	}

	@Override
	public void keyPressed(KeyEvent e) 
	{
		if(e.getKeyCode() == KeyEvent.VK_A)
			main.taskManager.taskList.add(new ActivateLifeSupport(this));
	}

	@Override
	public void keyReleased(KeyEvent e) 
	{
		
	}

	@Override
	public void keyTyped(KeyEvent e) 
	{
		
	}
}
